﻿using System.Linq;
using System.Reflection;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using WindowsPhoneMVC.ActionResults;
using WindowsPhoneMVC.Framework;
using WindowsPhoneMVC.Navigation;

namespace WindowsPhoneMVC.Tests.Navigation
{
    [TestClass]
    public class DefaultActionMatcherTests
    {
        private MethodInfo[] methods;
        private ActionMatcherController testController;
        private DefaultActionResolver resolver;

        [TestInitialize]
        public void Setup()
        {
            methods = typeof (ActionMatcherController).GetMethods();
            resolver = new DefaultActionResolver();
            testController = new ActionMatcherController();
        }

        [TestMethod]
        public void MatchesExact()
        {
            // arrange
            var exactMatch = methods.Where(m => m.Name == "ExactMatch");

            // act
            var bestMatch = resolver.GetBestMatch(exactMatch.ToArray(), new NavigationRequest(null, null, "ExactMatch"));
            bestMatch(testController);

            // assert
            Assert.IsTrue(testController.ExactMatchInvoked);
        }

        [TestMethod]
        public void MatchesOptionalDataContext()
        {
            // arrange
            var exactMatch = methods.Where(m => m.Name == "NavigateToExample");

            // act
            var bestMatch = resolver.GetBestMatch(exactMatch.ToArray(), new NavigationRequest(null, null, "NavigateToExample"));
            bestMatch(testController);

            // assert
            Assert.IsTrue(testController.NavigateToExampleInvoked);
        }
 
        public class ActionMatcherController : Controller
        {
            public bool ExactMatchInvoked;
            public ActionResult ExactMatch()
            {
                ExactMatchInvoked = true;
                return null;
            }

            public bool NavigateToExampleInvoked;
            public ActionResult NavigateToExample()
            {
                NavigateToExampleInvoked = true;
                return null;
            }
        }
    }
}